﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.DAL;
using Silvermoon.Controls.Forms;
using System.Drawing;
using Silvermoon.Windows.Styles;
using Passwords.Utils;
using System.Threading;

namespace Passwords.Pages
{
    public class ChangePasswordPage : Page
    {
        private TextBox currentPasswordBox;
        private TextBox newPasswordBox;
        private TextBox confirmPasswordBox;
        private Toolbar toolbar;
        private AnimatedControl anim;

        public ChangePasswordPage()
            : base()
        {
            InitializePage();
        }

        private void InitializePage()
        {
            const int bs = 16;
            Title = "CHANGE PASSWORD";
            Styles = StylePool.Metro;
            Styles.SetClass<MenuItem>(new TextItemStyle { Font = FontCache.MediumRegularFont });

            ScrollPanel panel = new ScrollPanel { Margin = Margin.Zero };
            anim = new AnimatedControl { Size = new Size(128, 128), Margin = new Margin(true, false) { Top = 420 }, Visible = false, StartDelay = 0 };
            Controls.Add(panel, anim);

            Label l1 = new Label { Text = "Current password", Location = new Point(16, 0), AutoSize = true };
            Label l2 = new Label { Text = "New password", Location = new Point(16, 112), AutoSize = true };
            Label l3 = new Label { Text = "Confirm password", Location = new Point(16, 236), AutoSize = true };
            currentPasswordBox = new PasswordBox { Margin = new Margin(bs, 36, bs, null), Height = 64 };
            newPasswordBox = new PasswordBox { Margin = new Margin(bs, 24 + 124, bs, null), Height = 64 };
            confirmPasswordBox = new PasswordBox { Margin = new Margin(bs, 24 + 248, bs, null), Height = 64 };
            panel.Controls.Add(l1, l2, l3, currentPasswordBox, newPasswordBox, confirmPasswordBox);
            toolbar = new Toolbar();
            toolbar.Buttons.Add(new Button { Content = "Change", Width = 190 });
            toolbar.Buttons.Add(new Button { Content = "Cancel", Width = 190, Command = Page.BackCommand });

            toolbar.Buttons[0].Tap += new EventHandler<TapEventArgs>(ChangePasswordPage_Tap);
            Toolbar = toolbar;

            newPasswordBox.TextChanged += OnTextChanged;
            confirmPasswordBox.TextChanged += OnTextChanged;
        }

        protected override void OnShown()
        {
            base.OnShown();
            IsKeyboardVisible = true;
            currentPasswordBox.Focus();
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
            toolbar.Buttons[0].Enabled = false;
        }



        void OnTextChanged(object sender, EventArgs e)
        {
            toolbar.Buttons[0].Enabled = newPasswordBox.Text.Length > 3 && newPasswordBox.Text == confirmPasswordBox.Text;
        }

        void ChangePasswordPage_Tap(object sender, TapEventArgs e)
        {
            if (newPasswordBox.Text == confirmPasswordBox.Text)
            {
                IsKeyboardVisible = false;
                anim.Visible = true;
                anim.Enabled = true;
                string newPw = newPasswordBox.Text;
                string currentPw = currentPasswordBox.Text;
                newPasswordBox.Text = "";
                confirmPasswordBox.Text = "";
                currentPasswordBox.Text = "";

                ThreadPool.QueueUserWorkItem((o) =>
                    {
                        Thread.Sleep(0);
                        Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
                        Thread.Sleep(0);
                        if (!DalContext.ChangedPassword(currentPw, newPw))
                        {
                            Invoke(() =>
                                {
                                    anim.Visible = false;
                                    MessageDialog.Show("Database Error", "Could not change password.", null, null);
                                });
                        }
                        else Invoke(() => GoBack());
                    });
            }
        }
    }
}
